"""
Case Type   : HTAP行列融合特性
Case Name   : 设置列缓存所使用的缓冲区的大小max_imcs_cache设置方式验证
Create At   : 2024/12/25
Owner       : ningyali
Description :
    1.gs_guc set
    2.gsql中ALTER SYSTEM SET paraname TO value
    3.清理数据环境
Expect      :
    1.执行成功
    2.执行成功
    3.执行成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro


class HTAP(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('SingleDbUser')
        self.Constant = Constant()
        self.common = Common()
        self.values = {204800: True, 102400: True, 2147483647: True,
                       'null': False, '\'\'': False, '1.1': False,
                       102399: False, 2147483648: False, '\'熊猫\'': False}

    def test_trans(self):
        text = '----step1:gs_guc set;expect:执行成功----'
        self.log.info(text)
        for param, expect in self.values.items():
            guc_res = self.pri_sh.execute_gsguc(
                'set', self.Constant.GSGUC_SUCCESS_MSG,
                f'max_imcs_cache={param}')
            self.assertEqual(expect, guc_res, f'guc参数配置失败:{text}')

        text = '----step2:gsql中ALTER SYSTEM SET paraname TO value;' \
               'expect:执行成功----'
        self.log.info(text)
        for param, expect in self.values.items():
            sql = f'alter system set max_imcs_cache to {param};'
            sql_res = self.pri_sh.execut_db_sql(
                sql, env_path=macro.SINGLE_DB_ENV_PATH)
            if expect:
                self.assertIn('ALTER SYSTEM SET', sql_res, f'gsql参数配置失败:{text}')
            else:
                self.assertIn('ERROR', sql_res, f'gsql参数配置失败:{text}')

    def tearDown(self):
        text = '----step3:清理环境;expect:执行成功----'
        self.log.info(text)
        guc_res = self.pri_sh.execute_gsguc(
            'set', self.Constant.GSGUC_SUCCESS_MSG,
            f'max_imcs_cache')
        is_restarted = self.pri_sh.restart_db_cluster()

        self.assertTrue(guc_res, f'参数配置失败:{text}')
        self.assertTrue(is_restarted, f'重启数据库失败:{text}')

        self.log.info(f'-----{os.path.basename(__file__)} end-----')
